/*
 *  pricePrices.c
 *  PriceCompareDS
 *
 *  Created by Stacy Haponik on 5/27/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fat.h>
//#include <curses.h>
#include <nds.h>
#ifndef utilities.c
#define utilities.c
#endif

typedef struct {
	char date[10];
	char store[20];
	char itemType[10];
	char itemName[20];
	int	size;
	char unit[3];
	double price;
	int sale;
	int coupon;
	char notes[200];
} aPrice;

aPrice arrayOfPrices[50];					//TODO: fix this, size appropriately

int priceCount=0;

int numberOfPrices(void){					//determines how many prices are in prices.txt
	FILE *myFile;
	myFile=fopen("fat:prices.txt", "r"); 
	if (!myFile){
		iprintf("Couldn't open prices.txt for reading\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3");
		return 0;
	}
	else {
		int i=numberOfLinesInFile(myFile);
		fclose(myFile);
		return i-1;
	}
}

int readInPrices(void){
	FILE *myFile;
	myFile=fopen("fat:prices.txt", "r"); 
	if (!myFile){
		iprintf("Couldn't open prices.txt for reading.");
		return 0;
	}
	else {
		priceCount=numberOfPrices();		//fix this-- number of prices should return -1 if it fails.
		iprintf("Number of Prices: %i",priceCount);
	}
	char myChar;
	myFile=fopen("fat:prices.txt", "r");
	//myPrices arrayOfPrices[storeCount];			//make an array of store structs, of size to fit all the stores
	int j=0;
	myChar = fgetc(myFile);
	while ( myChar!='\n'){		//loop through till the end of the first line, since it's just a header
		myChar = fgetc(myFile);
	}
	for (j=0; j < priceCount; j++){			//loop until all prices are added to the arrayOfPrices
		//		iprintf("In the loop processing statements");
		int i=0;
		int column=1;		//what column are we on?
		while (column<11){
			//				iprintf("\nIn the while loop, column=%i",column);
			int exit=0;
			switch(column){
				case 1:		//date
					iprintf("\ndate: ");
					for (i=0; i <= 10&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].date,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					
					case 2:		//store
					iprintf("\nstore: ");
					for (i=0; i <= 20&&exit==0; i++){
						char myString[20];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].store,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					
					case 3:		//itemType
					iprintf("\nitemType: ");
					for (i=0; i <= 10&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].itemType,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					case 4:		//itemName
					iprintf("\nitemName: ");
					for (i=0; i <= 20&&exit==0; i++){
						char myString[20];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].itemName,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					//works up to here... does it work past here?
					case 5:		//size
					iprintf("\nSize: ");
					for (i=0; i <= 4&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							arrayOfPrices[j].size=atoi(myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					case 6:						//unit
					iprintf("\nUnit: ");
					for (i=0; i <= 10 &&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].unit,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					case 7:						//price
					iprintf("\nPrice: ");
					for (i=0; i <= 10&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							arrayOfPrices[j].price=atof(myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					case 8:		//sale
					iprintf("\nSale?: ");
					for (i=0; i <= 1&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							arrayOfPrices[j].sale=atoi(myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					case 9:		//coupon
					iprintf("\nCoupon?: ");
					for (i=0; i <= 1&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							arrayOfPrices[j].coupon=atof(myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					if (myChar=='\n'){column=20;}
					break;
					/*
					case 10:	//notes
					iprintf("\nNotes: ");
					for (i=0; i <= 200&&exit==0; i++){
						char myString[10];
						myChar = fgetc(myFile);
						if ((myChar == '\t')||( myChar=='\n')){
							myString[i]='\0';
							iprintf("%s",myString);
							strcpy(arrayOfPrices[j].notes,myString);
							exit=1;
						}
						else myString[i]=myChar;
					}
					*/
					if (myChar=='\n'){column=20;}
					break;
					default:
					iprintf("Reached end of row %i\n",j);
					column=20;
					exit=1;
					i=0;
			}
			column++;
			//			iprintf("\nColumn=%i",column);
		}
		//		iprintf("Row %i. PriceCount=%i\n",j, priceCount);
	}
	fclose(myFile);
	iprintf("End of readInPrices\n");
	return 0;
}

int printPrices(void){
	iprintf("begin of printPrices\n");
	//	readInPrices();
	readInPrices();
	iprintf("past readInPrices\n");
	int i=0;
	//	iprintf("i=%i. priceCount=%i\n",i,priceCount);
	for (i=0; i<priceCount; i++){	//loop until we reach the last price
		printf("%s (%s)\n     $%.2f ($%.2f/%s) \n",arrayOfPrices[i].itemName,arrayOfPrices[i].itemType,arrayOfPrices[i].price, (arrayOfPrices[i].price/arrayOfPrices[i].size),arrayOfPrices[i].unit);
	}
	return 0;
}

int priceScreen(){
	printPrices();
	int addPrice=0;	//if true, then we should display the "add price" keyboard and questions.
	int i=0;		//iterator to store our spot in the "add price" buffer.
	int question=1;		//which question are we on?
	int maxQuestion=10;	//how many questions do I have?
	int prompt=0;		//is it time to ask a question?
	int answerLength=0;
	int maxAnswerLength=20;	//how long of an answer should the prompt accept?
	keyboardDemoInit();
	//	keyboardHide();
	//	keyboardShow();
	
	char buffer[sizeof(aPrice)];	//add store buffer-- size is maximum possible size for a line to be saved to prices.txt.
	iprintf("%i",sizeof(aPrice));
	while(1){		//loop until you get input
		scanKeys();
		int pressed = keysDown();   // buttons pressed this loop
		
		if ( pressed & KEY_A){
			//			consoleClear();
			addPrice=1;
			prompt=1;
			//			printPrices();
			keyboardShow();
			iprintf("Let's add a price entry!");
		}		//add a store
		if ( pressed & KEY_L){
			iprintf("Swapping to shopping list screen");
			return 0;
			break;
		}		//swap to "price entries" screen
		if ( pressed & KEY_R){
			iprintf("Swapping to Stores screen");
			return 2;
			break;
		}		//swap to calculator mode
		int key = keyboardUpdate();
		
		if (addPrice==1){						//TODO FIX THIS
			//			consoleSelect(&bottomScreen);
			if (prompt==1){
				prompt=0;
				switch (question){
					case 1:
						iprintf("\nDate: ");
						break;
					case 2:
						iprintf("\nStore: ");
						break;
					case 3:
						iprintf("\nType: ");
						break;
					case 4:
						iprintf("\nItem: ");
						break;
					case 5:
						iprintf("\nSize: ");
						break;
					case 6:
						iprintf("\nUnitss: ");
						break;
					case 7:
						iprintf("\nPrice: ");
						break;
					case 8:
						iprintf("\nSale?: ");
						break;
					case 9:
						iprintf("\nCoupon?: ");
						break;
					case 10:
						iprintf("\nNotes: ");
						break;
					default:
						iprintf("\nOoops. I don't know how I got here.");
						break;
				}
			}			//print the appropriate prompt
			
			
	/*	char date[10];
	char store[20];
	char itemType[10];
	char itemName[20];
	int	size;
	char unit[3];
	double price;
	int sale;
	int coupon;
	char notes[200];
	*/
			
			if (key>0&&key!=255){
				//					iprintf("%c", key);
				if ((key==10)||(answerLength>=maxAnswerLength)){
					buffer[i]='\t';
					prompt=1;
					question++;
					i++;
					answerLength=0;
					//					iprintf("Adding a tab");
				}		//if there's a return character
				else if(key>=32){
					buffer[i]=key;
					iprintf("%c",key);
					i++;
					answerLength++;
				}		//if there's a letter, number, or space
				else if(key==8){
					i--;
					answerLength--;
					iprintf("%c",key);
				}		//if there's a backspace character
				if (question>maxQuestion){
					i--;		//remove the trailing tab character
					buffer[i]='\0';
					question=1;
					addPrice=0;
					i=0;
					answerLength=0;
					FILE *myFile=fopen("fat:prices.txt", "a"); 
					fprintf(myFile,"\n%s",buffer);
					fclose(myFile);
					consoleClear();
					iprintf("\nAdded Price: %s\n",buffer);
					printPrices();
					keyboardHide();
				}				//print to file, reset iterators
				key=0;
			}
			//			consoleSelect(&topScreen);
		}		//loop to add a price entry
		swiWaitForVBlank();
	}
}
